﻿using System;
using Autofac;
using Laugh.Data.DatabaseCfg;
using Laugh.Data;
using Laugh.Environment;

namespace Laugh.DependencyInjection
{
    internal static class Ioc
    {
        public static IContainer Container { get; private set; }

        public static void Run(Action<ContainerBuilder> before = null)
        {
            var builder = new ContainerBuilder();

            //register modules
            builder.RegisterModule(new EnvironmentModule());
            builder.RegisterModule(new DatabaseModule());

            if (before != null)
                before(builder);

            Container = builder.Build();

            //register cfg Databases
            Container.Resolve<IDatabasePublisher>().Register();
        }

        public static void Stop()
        {
            if (Container != null)
                Container.Dispose();
        }
    }
}
